//
//  WJPublishPicVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/30.
//  Copyright © 2016年 王景伟. All rights reserved.
//
//  图片 √

#import "WJPublishPicVC.h"
#import "WJTextView.h"
#import "WJDataManager.h"
#import "SVProgressHUD.h"
#import "WJPublishModel.h"

@interface WJPublishPicVC ()<UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) WJBaseView *touView;

@property (nonatomic, retain) WJTextView *textView;

@property (nonatomic, strong) WJBaseButton *rightbutton;

@property (nonatomic, strong) UIImageView *picView;

@property (nonatomic, strong) WJBaseButton *chooseButton;

@end

@implementation WJPublishPicVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.textView becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
    
    [self createTextView];
}

- (void)createView
{
    self.touView = [[WJBaseView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.view addSubview:self.touView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 24, WIDTH - 200, 40)];
    lable.text = @"发表图片";
    lable.textAlignment = 1 ;
    [self.touView addSubview:lable];
    
    WJBaseButton *button = [WJBaseButton buttonWithTitle:nil frame:CGRectMake(20, 30, 50, 30) target:self action:@selector(puLeftAction)];
    button.backgroundColor = BSClearColor;
    [button setImage:[UIImage imageNamed:@"navigationButtonReturn.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick.png"] forState:UIControlStateHighlighted];
    [self.touView addSubview:button];
    
    self.rightbutton = [WJBaseButton buttonWithTitle:@"发表" frame:CGRectMake(WIDTH - 70, 30, 50, 30) target:self action:@selector(puRightAction)];
    [ self.rightbutton setTitleColor:BSBlackColor forState:0];
    self.rightbutton.backgroundColor = BSClearColor;
    [self.touView addSubview: self.rightbutton];
    [self.touView layoutIfNeeded];
    
}

- (void)createTextView
{
    self.textView = [[WJTextView alloc]initWithFrame:CGRectMake(15, 64, WIDTH - 30, HEIGHT * 2 / 7)];
    [self.view addSubview:self.textView];
    _textView.alwaysBounceVertical = YES;
    _textView.delegate = self;
    _textView.placeholder = @"这里添加文字,请勿发布色情、政治等违反国家法律的内容,违者封号处理";
    self.editing = YES;
    self.textView.userInteractionEnabled = YES;
    
    
    self.picView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 64 + HEIGHT * 2 / 7, 100, 100)];
    self.picView.backgroundColor = BSGrayColor;
    [self.view addSubview:self.picView];
    
    self.chooseButton = [WJBaseButton buttonWithType:UIButtonTypeSystem];
    self.chooseButton.frame = CGRectMake(150, 64 + HEIGHT * 2 / 7 + 30, 80, 30);
    [self.chooseButton setTitle:@"选择图片" forState:UIControlStateNormal];
    [self.chooseButton addTarget:self action:@selector(buttonAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.chooseButton];
    
}

- (void)puLeftAction
{
    [self.textView resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)buttonAct:(WJBaseButton *)button{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.allowsEditing =YES;
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:^{}];  //跳向自己的相册
        
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"自拍" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        // 可以选择自拍
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        
        //判断是否可以打开照相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
        } else {
           UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"当前设备不支持拍照功能" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {}];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:^{}];

        }
        [self presentViewController:picker animated:YES completion:nil];

    }];
    [alert addAction:action];
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:^{}];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    // 如果是拍照功能 则将照片存入相册
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //图片存入相册
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }

    self.picView.image = image;
}

//  点击发表进入本地
- (void)puRightAction
{
    [self.textView resignFirstResponder];
    
    NSTimeInterval time=[[NSDate date] timeIntervalSince1970];
    // 美国时间
    NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    // 北京时间
    NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
    
    WJPublishModel *model = [[WJPublishModel alloc]init];
    model.type = @"pic";
    model.text = self.textView.text;
    model.nowTime = currentDateStr;
    model.picData = UIImageJPEGRepresentation(self.picView.image, 1);
    [[WJDataManager shareFMDB]insertModel:model];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发表成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{}];
    
}


#pragma mark -- UITextFieldDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //拖拽时 结束编辑 退出键盘
    [self.textView endEditing:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.textView.frame = CGRectMake(15, 64, WIDTH - 30, HEIGHT - 64 - 150);
        self.picView.frame = CGRectMake(10, HEIGHT - 150, 100, 100);
        self.chooseButton.frame = CGRectMake(150, HEIGHT - 100, 80, 30);

    }];
}

- (void)textViewDidChange:(UITextView *)textView{
    //判读是否有文字  设置发表按钮的状态
    self.rightbutton.enabled = textView.hasText;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [UIView animateWithDuration:0.5 animations:^{
        self.textView.frame = CGRectMake(15, 64, WIDTH - 30, HEIGHT * 2 / 7);
        self.picView.frame = CGRectMake(10, 64 + HEIGHT * 2 / 7, 100, 100);
        self.chooseButton.frame = CGRectMake(150, 64 + HEIGHT * 2 / 7 + 30, 80, 30);
    }];
    return YES;
}



@end
